<?php
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use backend\models\Human;
use backend\models\FilesForm;
use backend\models\Files;
use backend\models\Category;
class HumanController extends BaseController{
	public $nodeName = '人物管理';
	public $init_upload_image = '/common/uploads/system/upload.jpg';

	public function init(){
        parent::init();
	}

	public function actionIndex(){
		
	}

	public function actionLists(){
		$lists = Human::find()->asArray()->all();
		return $this->render('lists', ['lists' => $lists]);
	}

	public function actionCreate(){
		$id = $this->request->get('id');

		if(!empty($id)){
			$human = Human::findOne($id);
			$human->setScenario('update');
		}else{
			$human = new Human;
			$human->setScenario('create');
		}

		if($this->request->isPost){
			if($human && $human->load($this->request->post())){
				if($human->save()){
					$this->session->setFlash('success', '保存成功!');
					return $this->goBack();
				}else{
					$this->session->setFlash('error', '保存失败!');
				}
			}else{
				$this->session('error', '???????!!!!!');
			}

		}

		/**
		 * 创建时image_id 一定是 空, 更新时 可能是空
		 */

		$result = [
			'human' => $human,
			'pageTitle' => '添加人物',
			'clists' => Category::find()->all(),
		];

		return $this->render('data-form', $result);
	}

	public function actionEdit(){

	}

	public function actionDel(){

	}

	public function actionAjaxDel(){

	}

	public function actionAjaxGetOne(){

	}

	public function actionAjaxGetList(){

	}

	public function actionSearch(){
		Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
		$out = ['results' => ['id' => '', 'text' => '']];

		/*if (!$q) {
	        return $out;
	    }*/

	    $data = Human::find()
	                ->select('id, name AS text')
	                ->asArray()
	                ->all();
	                
	    $out['results'] = array_values($data);

	    return $out;
	}
}